package edu.cnu.tpts.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import edu.cnu.tpts.bean.*;
import edu.cnu.tpts.dao.*;
import edu.cnu.tpts.util.DBManager;

public class AdminDaoImpl implements AdminDao {

	public List findAllTeacher() {
		TeacherDao teaDao = new TeacherDaoImpl();
		return teaDao.getAll();
		
	}

	public List<Student> findAllStudent() {
		// TODO Auto-generated method stub
		StudentDao stuDao = new StudentDaoImpl();
		return stuDao.getAll();
	}

	public void addStudent(Student student) {
		StudentDao stuDao = new StudentDaoImpl();
		stuDao.addStudent(student);
		
	}

	public void addTeacher(Teacher teacher) {
		TeacherDao teaDao = new TeacherDaoImpl();
		teaDao.addTeacher(teacher);
		
	}

	public void updateStudent(Student student) {
		StudentDao stuDao = new StudentDaoImpl();
		stuDao.updateStudent(student);
		
	}

	public void updateTeacher(Teacher teacher) {
		TeacherDao teaDao = new TeacherDaoImpl();
		teaDao.updateTeacher(teacher);
		
	}

	public void deleteStudent(int stuid) {
		DBManager dbManager = new DBManager();
		Connection conn=dbManager.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			
			pstmt=conn.prepareStatement("delete from student_score where stu_id="+stuid);
			pstmt.execute();
			pstmt=conn.prepareStatement("delete from student_course where stu_id="+stuid);
			pstmt.execute();
			pstmt=conn.prepareStatement("delete from student where id="+stuid);
			pstmt.execute();
		} catch (SQLException e) {
						e.printStackTrace();
		}finally{
			dbManager.closeAll(rs, pstmt, conn);
		}
		
	}

	public void deleteTeacher(int teaid) {
		DBManager dbManager = new DBManager();
		Connection conn=dbManager.getConnection();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			pstmt=conn.prepareStatement("delete from teacher_course where tea_id="+teaid);
			pstmt.execute();
			pstmt=conn.prepareStatement("delete from teacher where id="+teaid);
			pstmt.execute();
		} catch (SQLException e) {
						e.printStackTrace();
		}finally{
			dbManager.closeAll(rs, pstmt, conn);
		}
	
		
	}

	public static void main(String[] args) {
		AdminDao adDao = new AdminDaoImpl();
		//adDao.deleteTeacher(41);
		adDao.deleteStudent(70479);
	}
}
